import axios from "axios"

export function request(config) {
    return new Promise((resolve, reject) => {
        //创建axios实例
        const instance = axios.create({
            baseURL: 'http://httpbin.org',
            timeout: 10 * 1000
        })

        //拦截request
        instance.interceptors.request.use(
            config => {
                console.log('拦截请求');
                console.log(config);
                //返回config
                return config
            },
            err => {
                console.log(err);
                return err
            })
        //拦截响应数据
        instance.interceptors.response.use(
            response => {
                console.log('拦截响应');
                console.log(response);
                return response
            },
            err => {
                return err
            }
        )

        //发送请求
        instance(config).then(res => {
            resolve(res)
        })
            .catch(err => {
                reject(err)
            })
    })
}

//第二个实例
export function requests(config) {
    return new Promise((resolve, reject) => {
        //创建axios实例
        const instance = axios.create({
            baseURL: 'http://159.75.112.81:8080',
            // baseURL: 'http://localhost:8080',
            timeout: 10 * 1000,
            headers: {
                'MaoyanToken': localStorage.getItem("tokenValue"),
                'content-type': 'application/json'
            },
            withCredentials: true,
        })

        //拦截request
        instance.interceptors.request.use(
            config => {
                console.log('拦截请求');
                console.log(config);
                //返回config
                return config
            },
            err => {
                console.log(err);
                return err
            })
        //拦截响应数据
        instance.interceptors.response.use(
            response => {
                console.log('拦截响应');
                console.log(response);
                return response
            },
            err => {
                return err
            }
        )

        //发送请求
        instance(config).then(res => {
            resolve(res)
        })
            .catch(err => {
                reject(err)
            })
    })
}